package com.zgs.mall.order.listener;

import com.rabbitmq.client.Channel;
import com.zgs.mall.order.entity.OrderEntity;
import com.zgs.mall.order.service.OrderService;
import com.zgs.mall.order.to.SeckillOrderTo;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@RabbitListener
@Component
public class OrderKillListener {
    @Autowired
    OrderService orderService;
    @RabbitHandler
    public  void listener(SeckillOrderTo orderTo, Channel channel, Message message) throws IOException {
        try {
            //创建秒杀单的详细信息
            orderService.createSeckillOrder(orderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
